package com.cmwa.solution.business.infodisclosure.regulatoryreport.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.infodisclosure.institution.entity.InstitutionVo;
import com.cmwa.solution.business.infodisclosure.regulatoryreport.dao.RegulatoryReportDao;
import com.cmwa.solution.business.infodisclosure.regulatoryreport.entity.RegulatoryReportVo;
import com.cmwa.solution.business.infodisclosure.regulatoryreport.service.RegulatoryReportService;
import com.cmwa.solution.sys.attach.dao.AttachDao;
import com.cmwa.solution.sys.attach.dao.AttachRelExtDao;
import com.cmwa.solution.sys.attach.entity.AttachRelExtVo;
import com.cmwa.solution.sys.attach.entity.AttachVo;
import com.cmwa.solution.utils.SessionUtils;

@Service
public class RegulatoryReportServiceImpl implements RegulatoryReportService {

	@Resource
	private RegulatoryReportDao regulatoryReportDao;
	
	@Resource
	private AttachDao attachDao;
	
	@Resource
	private AttachRelExtDao attachInfoExtDao;
	
	@Override
	public List<Map<String,String>> getReportChart(SearchParam param) {
		param.getSp().put("empType", SessionUtils.getEmployee().getEmpType());
		param.getSp().put("company", SessionUtils.getEmployee().getCompany());
		return regulatoryReportDao.getReportChart(param);
	}
	
	@Override
	public Page<RegulatoryReportVo> getRegulatoryReportListPage(SearchParam param) {
		List<RegulatoryReportVo> list = regulatoryReportDao.getRegulatoryReportListPage(param);
		return Page.create(list, param.getStart(), param.getLimit(), param.getTotal());
	}

	@Override
	public void addRegulatoryReport(RegulatoryReportVo regulatoryReportVo, String attIds) {
		String empId = SessionUtils.getEmployee().getID();
		regulatoryReportVo.setCreateId(empId);
		regulatoryReportDao.addRegulatoryReport(regulatoryReportVo);
		if(StringUtils.isBlank(attIds)) {
			return;
		}
		String regulatRepId = regulatoryReportVo.getReportId();
		attachDao.addAttachRel2(empId, regulatRepId, "REGULAT_REPORT", attIds);
	}

	@Override
	public void regulatoryReportBatchDown(SearchParam sp, OutputStream out) throws Exception {
		List<RegulatoryReportVo> attList = regulatoryReportDao.getReportAttInfo(sp);
		if(CollectionUtils.isEmpty(attList)) {
			return;
		}
		Map<String,List<RegulatoryReportVo>> map = new HashMap<String,List<RegulatoryReportVo>>();
		for(RegulatoryReportVo regulatoryReportVo:attList) {
			String prdCode = regulatoryReportVo.getPrdSName();
			//特殊处理为空数据
			if(StringUtils.isBlank(prdCode)) {
				prdCode = "-";
			}
			if(map.containsKey(prdCode)) {
				map.get(prdCode).add(regulatoryReportVo);
				map.put(prdCode, map.get(prdCode));
			}else {
				List<RegulatoryReportVo> list = new ArrayList<RegulatoryReportVo>();
				list.add(regulatoryReportVo);
				map.put(prdCode, list);
			}
		}
		ZipOutputStream outZip = new ZipOutputStream(out);
		try {
			for (Map.Entry<String,List<RegulatoryReportVo>> entry : map.entrySet()) {
				List<RegulatoryReportVo> list = entry.getValue();
				if(CollectionUtils.isNotEmpty(list)) {
					for (int i = 0; i < list.size(); i++) {
						outZip.putNextEntry(new ZipEntry(entry.getKey()+"/"+list.get(i).getAttName()));
						outZip.setEncoding("UTF-8");
						FileInputStream fis1 = null;
						try {
							fis1 = new FileInputStream(list.get(i).getAttPath());
							if(fis1!=null){
								byte[] buffer1 = new byte[1024];
								int byteread1 = 0;
								// 写入zip包
								while ((byteread1 = fis1.read(buffer1)) != -1) {
									outZip.write(buffer1, 0, byteread1);
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}finally{
							if(fis1!=null){
								fis1.close();
							}
						}
					}
				}
			}
		} catch (Exception ex2) {
			ex2.printStackTrace();
			throw new Exception("关闭大对象异常");
		}finally{
			if (outZip != null) {
				outZip.flush();
				outZip.close();
			}
		}
	}

	@Override
	public void addRegulatoryReportNew(RegulatoryReportVo regulatoryReportVo, String attIds, String flag) {
		String empId = SessionUtils.getEmployee().getID();
		regulatoryReportVo.setModifyId(empId);
		regulatoryReportDao.updateRegulatoryReport(regulatoryReportVo);
		
		String regulatRepId = regulatoryReportVo.getReportId();
		List<String> oldRelIds = attachDao.queryRelIdExtFlag(regulatRepId, "REGULAT_REPORT","regulatoryAttFlag",flag);
		attachDao.deleteAttachRelExtFlag(regulatRepId,"REGULAT_REPORT","regulatoryAttFlag",flag);
		if(!CollectionUtils.isEmpty(oldRelIds)) {
			String oldRelIdStr = StringUtils.join(oldRelIds,",");
			attachDao.deleteAttachRelFlag(regulatRepId, "REGULAT_REPORT",oldRelIdStr);
		}
		if(StringUtils.isBlank(attIds)) {
			return;
		}
		
		List<AttachRelExtVo> list = new ArrayList<AttachRelExtVo>();
		String[] attIdsArr = attIds.split(",");
		AttachRelExtVo attachRelExtVo = null;
		
		String relType = "REGULAT_REPORT";
		String attrName = "regulatoryAttFlag";
		
		for(String attId : attIdsArr){
			attachRelExtVo = new AttachRelExtVo(null, null, attrName, flag, null, empId, null, null, null, attId, regulatRepId, relType);
			list.add(attachRelExtVo);
		}
		attachInfoExtDao.batchInsertAttachRelAndExt(list);
	}

	@Override
	public RegulatoryReportVo getRegulatoryReportInfoNew(SearchParam param) {
		return regulatoryReportDao.getRegulatoryReportInfoNew(param);
	}
	
	@Override
	public Page<RegulatoryReportVo> getRegulatoryReportListPageNew(SearchParam param) {
		param.getSp().put("empType", SessionUtils.getEmployee().getEmpType());
		param.getSp().put("company", SessionUtils.getEmployee().getCompany());
		List<RegulatoryReportVo> list = regulatoryReportDao.getRegulatoryReportListPageNew(param);
		return Page.create(list, param.getStart(), param.getLimit(), param.getTotal());
	}

	@Override
	public List<AttachVo> getReportAttInfoNew(SearchParam param) {
		return regulatoryReportDao.getReportAttInfoNew(param);
	}

	@Override
	public void regulatoryReportDownNew(SearchParam sp, OutputStream out) throws Exception {
		List<AttachVo> attList = regulatoryReportDao.getReportAttInfoNew(sp);
		/*if(CollectionUtils.isEmpty(attList)) {
			return;
		}*/
		ZipOutputStream outZip = new ZipOutputStream(out);
		outZip.setEncoding("UTF-8");
		try {
			if(CollectionUtils.isNotEmpty(attList)) {
				for (int i = 0; i < attList.size(); i++) {
					outZip.putNextEntry(new ZipEntry(attList.get(i).getAttName()));
					FileInputStream fis1 = null;
					try {
						File file = new File(attList.get(i).getAttPath());
						if(file.exists()) {
							fis1 = new FileInputStream(attList.get(i).getAttPath());
							if(fis1!=null){
								byte[] buffer1 = new byte[1024];
								int byteread1 = 0;
								// 写入zip包
								while ((byteread1 = fis1.read(buffer1)) != -1) {
									outZip.write(buffer1, 0, byteread1);
								}
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}finally{
						if(fis1!=null){
							fis1.close();
						}
					}
				}
			}
		} catch (Exception ex2) {
			ex2.printStackTrace();
			throw new Exception("关闭大对象异常");
		}finally{
			if (outZip != null) {
				outZip.flush();
				outZip.close();
			}
		}
	}

	@Override
	public List<String> getRegulatoryReportIdNew(SearchParam param) {
		param.getSp().put("empType", SessionUtils.getEmployee().getEmpType());
		param.getSp().put("company", SessionUtils.getEmployee().getCompany());
		return regulatoryReportDao.getRegulatoryReportIdNew(param);
	}

	@Override
	public List<InstitutionVo> getReportInstList() {
		return regulatoryReportDao.getReportInstList();
	}

	@Override
	public List<Map<String, String>> getReportTypesByInst(String instId, String reportName) {
		return regulatoryReportDao.getReportTypesByInst(instId, reportName);
	}

}
